using System;
using System.Collections.Generic;
using System.Text;
using IRQ.BaseFunctions.Drawing;

namespace IRQ.GPS.Distance
{
    /// <summary>
    /// Calculate distance between two coordinate points using orthodrome formula
    /// </summary>
    public class GPSCountDistance
    {
        /// <summary>
        /// Represents the value of following division Math.PI / 180 
        /// </summary>
        private const double Radian = 0.017453292519943295769222222222222;

        /// <summary>
        /// Calculates distance between two geo coordinate points.
        /// </summary>
        /// <param name="_pointFrom">Point, in degrees, from which to start calculating distance. X is longitude Y is latitude</param>
        /// <param name="_pointTo">Point, in degrees, to which we'll calculate distance. X is longitude Y is latitude</param>
        /// <returns></returns>
        public static double CalculateDistance(PointF _pointFromInDegrees, PointF _pointToInDegrees)
	    {

            double _YxRadianFrom = _pointFromInDegrees.Y * Radian;
            double _YxRadianTo = _pointToInDegrees.Y * Radian;
            double suma = Math.Sin(_YxRadianFrom) * Math.Sin(_YxRadianTo) +
                          Math.Cos(_YxRadianFrom) * Math.Cos(_YxRadianTo) * Math.Cos((_pointToInDegrees.X - _pointFromInDegrees.X) * Radian);
	
		    if(suma <=1 && suma >=-1)
			    return Math.Acos(suma);
		    else
			    return 0;
	    }

        /// <summary>
        /// Converts calculated distance from degrees to kilometers
        /// </summary>
        /// <param name="_distance">distance in degrees</param>
        /// <returns>distans in kilometers</returns>
	    public static double DistanceDeg2km(double _distance)
	    {
		    return _distance * 6371;
	    }
   
        /// <summary>
        ///Converts calculated distance from degrees to meters
        /// </summary>
        /// <param name="_distance">distance in degrees</param>
        /// <returns>distance in meters</returns>
	    public static double DistanceDeg2m(double _distance)
	    {
		    return DistanceDeg2km(_distance) * 1000;		 
	    }
    }
}
